package accounts;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/** @author aaronfrost */
import java.util.LinkedList;
import java.sql.*;
import powerpass.PowerPassSQL;


public class AccountCollectionBean implements java.io.Serializable {
    private int clientid;
    private LinkedList<BaseAccount> accounts;
    private int selectedIndex = -1;

    public void setSelectedIndex(int index) {
        this.selectedIndex = index;
    }
    public int getSelectedIndex() {
        return this.selectedIndex;
    }
    public AccountCollectionBean()
    {
    
    }

    public void setClientid(int inClientid) throws SQLException
    {
        clientid = inClientid;
        populateAccounts();
    }
    public int getClientid()
    {
        return clientid;
    }
    public LinkedList getAccounts()
    {
        return accounts;
    }
    
    public void setAccounts(LinkedList<BaseAccount> inAccounts)
    {
        accounts = inAccounts;
    }

    private void populateAccounts() throws SQLException
    {
        String SQL = "SELECT accountid, clientid,  active, createddt, modifieddt, name, username, description, accounttype "+
                     "FROM slccpowerpass.accounts "+
                     "WHERE clientid = "+ clientid +";";
        accounts = new LinkedList<BaseAccount>();
        //Select accounts from database.
        PowerPassSQL pp = new PowerPassSQL();
        ResultSet results = pp.selectInfo(SQL);
        while (results.next())
        {
            int accountid = results.getInt("accountid");
            boolean active = results.getBoolean("active");
            Date createddt = results.getDate("createddt");
            Date modifieddt = results.getDate("modifieddt");
            String name = results.getString("name");
            String username = results.getString("username");
            String description = results.getString("description");
            int accounttype = results.getInt("accounttype");
            int clientid = results.getInt("clientid");
            
            if (accounttype == 1)
            {
                StandardAccount sa = new StandardAccount(accountid, clientid, active, createddt, modifieddt, name, username, description, accounttype);
                sa.populatePasswordHistory(accountid);
                accounts.add(sa);
            }
            else if (accounttype == 2)
            {
//                FinancialAccount fa = new FinancialAccount();
//                accounts.add(fa);
            }
        }
    }
}
